var rootPath = getRootPath();

$(function() {
	//滚动条置顶
	 $("body").prepend('<input type="text" id="focus" size="1">');
    $("#focus").focus();
   $("#focus").hide();
	var rleId = getUrlParam("rleId");
	$("#rleId").val(rleId);
	var prevPage = getUrlParam("currentPage");
	$.ajax({
		url: rootPath + '/verify/uid/rleId/' + rleId + '/dr/1',
		type: 'GET',
		success: function(data) {
			if('0000' == data.returnCode) {
				var verify = data.returnObject;
				$("#company").val(verify.company);
				$("#job").val(verify.job);
				$("#speciality").val(verify.speciality);
				$("#intro").val(verify.intro);
			}
		},
		error: function(jqXHR,textStatus,errorThrown) {
			layer.alert("error:(");
		}
	});
	
	$("#intro").bind("change,keydown,up", function() {
		$(this).val($(this).val().substring(0,800));
	});
	
	//上一步
	$("#prev").click(function() {
		var creType = $("#sort").val();
		parent.$("#content").prop("src", prevPage + "?rleId=" + rleId + "&creType=" + creType);
	});
	
	$("#applyAuth").click(function() {
		
		if(!checkData()){
			return;
		}
		
		//前端数据长度校验
		if($("#company").val().length > 50) {
			$("#company").focus();
			showErrMsg("company", "工作单位输入太长,请精简");
			return;
		}

		if($("#job").val().length > 50) {
			$("#job").focus();
			showErrMsg("job", "职业输入太长,请精简");
			return;
		}
		
		if($("#speciality").val().length > 50) {
			$("#speciality").focus();
			showErrMsg("speciality", "特长输入太长,请精简");
			return;
		}
		
		if($("#intro").val().length > 800) {
			$("#intro").focus();
			showErrMsg("intro", "简介输入太长,请精简");
			return;
		}
		
		showLoading();
		$("#volAuthForm").ajaxSubmit({
			url : rootPath + '/verify/saveOrUpdateVerify',
			type: 'POST',
			contentType : 'application/json;charset=UTF-8',
			success : function(data) {
				hideLoading();
				layer.msg("操作成功",{icon:1,time:1000},function() {
					parent.$("#content").prop("src","applyResult.html?rleId=" + rleId + "&title=志愿者认证");
				});
			},
			error: function (jqXHR, textStatus, errorThrown) {
				hideLoading();
				layer.alert("error");
			}
		});
		return false;
	});
});

function checkData(){
	if($.trim($("#company").val()) == '') {
		$("#company").focus();
		showErrMsg("company","请输入工作单位");
		return false;
	}
	if($.trim($("#job").val()) == '') {
		$("#job").focus();
		showErrMsg("job","请输入职业");
		return false;
	}
	if($.trim($("#intro").val()) == '') {
		$("#intro").focus();
		showErrMsg("intro","请输入简介");
		return false;
	}
	return true;
}